<?
/*fileName: 
 *internal version: 0001
 *version : 1.0.0 [programming] [official version]
 *author : HeShengyu
 *Date : 2014.07.21 
 *email: koolfer@163.com
 *end date:2014.08.23
 *http://www.cnblogs.com/ssnowy/
*/
/*
*文件中包类
 Clfn
 Clscandir
*/
//DEBUG_GRADE_ 的值越大越容易显示抛出信息
define('DEBUG_GRADE_',4);
//$nGrade数字越小越容易显示抛出信息示
function _debug($str,$nGrade=3){
    return $nGrade<DEBUG_GRADE_ ? $str : ""; 
}

class Clfn{
    public $root;
	public $httpdRoot;
	public $fileUrls;
    public function __construct($path=""){
		$this->root=Clfn::chSlashs($path);
		$this->fileUrls = array();		 
	}
	/*
	//chSlashs
	//$path 一般传入 __FILE__
	//把反斜杠替换成正斜杠，把多个正斜杠替换成一个
	*/
	public static function chSlashs ($path=""){
		$path=preg_replace( 
				array('/\/{1,}/'), 
				array('/'       ),
					str_replace( 
					   '\\','/' , 
					   dirname($path).DIRECTORY_SEPARATOR 
					)   
			);
		return $path;
	}
	public function __destruct(){
	}
	public function requireOnce($arrOrStr){
		if(is_array($arrOrStr)){
			foreach($arrOrStr as $v){
			    require_once($this->root.$v)  ;
			}	
			return;
		}
		if(is_dir($arrOrStr)){
			$this->requireDirOnce($arrOrStr);
			return;
			
		}
		if(!empty($arrOrStr)){
			require_once($this->root.$arrOrStr);
		}				
	}
	//$suffix 例如等于 "../../"."inc/"
	//通过路径包含php文件一次
	public   function requireDirOnce($suffix=''){	
	    $bool=function_exists('_debug');	
		$s=new clscandir($this->root.'/'.$suffix);
		$arr=$s->get_complete_files();	
		
		if($bool){
			foreach($arr as $v){
				if(preg_match("/\.php$/",$v) ){
			    echo _debug( _mkcstr("require_once('".$v."')</br>")->High('red',16)->Val() , 3 );			    }
			}	
		}
		foreach($arr as $v){
			if(preg_match("/\.php$/",$v) ){
			require_once($v) ;		
			}
		}
		
	}
	
	public function getHttpdRoot($str=""){
		if($str==""){return "";}
		$sRet=str_replace($str,"",$this->root);
		$this->httpdRoot=$sRet;
	    return  $this->httpdRoot;	
	}
	
	public function filePathToUrl($filePath=""){
		if($filePath==""){return "";}
	    $server_url=Clfn::iGetServerUrl();
		$ret = str_replace(
		$this->httpdRoot,$server_url,$filePath);
		$this->fileUrls[]=$ret;
		return $ret;
	}
	
	//internal 使用 
	public static function iGetServerUrl(){
        return 	"http://".$_SERVER[SERVER_NAME]
		.":".$_SERVER[SERVER_PORT]."/";
	}
	public static function echoarr($arr){
		if( !is_array($arr) ){ return false;}
		foreach($arr as $v){
		    echo $v."\n";	
		}
	}
	public static function resz($num ,$rate=1){
		
		return  ceil($num * $rate) ;
	}			
}

class Clscandir{
    public $path;
	public $sorting_order;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	//把文件转化为文件所在目录字串
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sorting_order=0){
		$path = Clscandir::fileToDirname($path);
		$this->scandir($path , $sorting_order);
		$this->get_complete_filesDirs();
		$this->get_complete_dirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path,$sorting_order=0){
		$this->path = $path ;
		$this->sorting_order = $sorting_order;
		$this->filesDirs=scandir ( $path , $sorting_order ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	public function get_complete_filesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = $this->_format_name($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	//得到完整的目录名和文件名  
	public function get_complete_dirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){
				
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function get_complete_files(){
		return $this->completeFiles;
	}
	private function iva(){}
	protected function ote(){}
	
	public function _scandir($path){
	    $dir = dir($path);
	    $retarr = array( ) ;
        while (($file = $dir->read()) !== false)
        {  $retarr[] =  ($file) ;  }	
	    $dir->close();
	    $retarr["cwd"]=$this->_format_name(getcwd());
	    return $retarr;
    }
	
	public function _format_name($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }
	
	public function show(){
		echo "目录：<br>";
	    secho($this->completeDirs);	
		echo "文件：<br>";
		secho($this->completeFiles);	
	}
	public function demo(){
	    echo str_replace(array("\n"," "),array("<br>","&nbsp;"),
	htmlspecialchars('     $dir=_default_em($_REQUEST["dir"],\"I:/AppServ/www/_doll/");
     $scandir=new CLsandir($dir,0);
     secho($scandir->get_complete_dirs());
     secho($scandir->get_complete_files());') )	;
		
	}
	public function each($arr,$callback=""){
		if(!is_array($arr))return 'Is not array!';
		
		$retarr=array();
		foreach($arr as $k=>$v){
			if(""!=$callback)
			{$retarr[$k]=$callback($v);	}
			else
			{$retarr[$k]=$v;}		
		}
		return $retarr;
	}
	public static function toHttpUrl($arr,
	$middle_path="/_doll/_ae/"){
		$severpath="http://".$_SERVER['SERVER_NAME']
		.":".$_SERVER['SERVER_PORT']
		.$middle_path;
        $retarr=array();
		foreach($arr as $k=>$v){
			 $v=preg_replace(
    //  "/[\d\D]*\/([a-zA-Z0-9_\.\-]*\.(?:flv|f4v|mp[4]|mkv))$/"
	  "/[\d\D]*\/([a-zA-Z0-9_\.\-\[\]\(\)]*\.(?:flv|f4v|mp[4]|mkv))$/"
       ,$severpath."\\1",$v);
	       if(!preg_match("/^[a-zA-Z]\:/",$v))
		   {
			   $retarr[$k]=Clscandir::replacePoundsign($v); 	
		   }	
		}
		return $retarr;		
	}
	public static function replacePoundsign($str){
        $str=iconv('GB2312','UTF-8', $str)  ;
	    $str= preg_replace("/#/","%23",$str);
		return $str;
	}	
}

function _mkcstr($str){
   return new Clstring($str);	
}
class Clstring{
	public $string;
    public function __construct($str){
		$this->string=$str;
	}
	public function Val(){
		return $this->string;
		}
	public function High($color="#f90",$size=20){
		$this->string=
		 '<span style="color:'.$color
		.';font-size:'
		.$size.'px;">'.$this->string.'</span>';
		return $this;
	}
	public function Fast($color="#f90",$size=20){
	    $this->High($color,$size)	;
		return $this->Val();
	}
	public function __destruct(){
		$this->string=NULL;
	}
	public function Add($str){
	   	$this->string.=$str;
		return $this;
	}	
}
?>